package com.hpin.mlxteacher.chat;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;


/**
 * 邀请加好友页面
 * Created by Aaron on 16/1/19.
 */
public class InviteContractActivity extends BaseActivity {

    private ImageView ivback;
    private TextView tvsend;
    private EditText etcontent;
    private String invitePhoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_invite_contract);

        invitePhoneNum = getIntent().getStringExtra("invitePhoneNum");

        this.etcontent = (EditText) findViewById(R.id.et_content);
        this.tvsend = (TextView) findViewById(R.id.tv_send);
        this.ivback = (ImageView) findViewById(R.id.iv_back);

        tvsend.setOnClickListener(this);
        ivback.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.tv_send://发送好友请求

                sendRequest();

                break;

            case R.id.iv_back:
                finish();
                break;

        }

    }

    /**
     * 发送好友请求
     */
    private void sendRequest() {

        com.alibaba.fastjson.JSONObject j = new com.alibaba.fastjson.JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("invitePhoneNum", invitePhoneNum);
        j.put("inviteWord", etcontent.getText().toString());

        HttpUtil.setPostRequest("发送中...", this, Constant.WEB_ROOT_TALK + Constant.INVITEFRIEND, j.toString(),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.SUCCESS) {
                                showToast("发送成功");
                                finish();
                            } else {
                                showToast(Constant.showErrorMsg(result.getInt("code"), InviteContractActivity.this));
                            }

                        } catch (Exception e) {
                            showToast("发送失败");
                            e.printStackTrace();
                        }

                    }
                }, null);

    }
}
